package com.arrogames.rgblabyrinth.menu.components {
	
	import com.arrogames.lib.graphics.Wedge;
	import com.arrogames.rgblabyrinth.AssetManager;
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.geom.Matrix;
	
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.krecha.PixelHitArea;
	import starling.extensions.krecha.PixelImageTouch;
	import starling.textures.Texture;
	
	public class ColorPicker extends Sprite {
		
		public static const COLOR_CHANGED:String = "ColorPicker::COLOR_CHANGED";
		
		private var colors:Array;
		private var radius:Number;
		
		private var _selectedColor:uint;

		private var wedges:Sprite;

		private var arc:Number;
		
		public function ColorPicker(colors:Array=null) {
			super();
			
			if (colors == null) {
				colors = [ 0xff0000, 0x00ff00, 0x0000ff ];
			}
			
			this.radius = 150;
			this.colors = colors;
			this._selectedColor = colors[0];
			
			arc = 360/colors.length;
			wedges= new Sprite();
			for (var i:int = 0; i < colors.length; i++) {
				var startAngle:Number = i * arc;
				wedges.addChild(createWedge(colors[i], arc, (i*arc)-(arc/2)));
			}
			wedges.pivotX = radius;
			wedges.pivotY = radius;
			this.addChild(wedges);
			
			var selectBorder:Image = new Image(AssetManager.instance.getTexture("color_select"));
			selectBorder.pivotX = radius;
			selectBorder.pivotY = radius;
			selectBorder.width = selectBorder.height = radius*2; 
			selectBorder.touchable = false;
			this.addChild(selectBorder);
		}
		
		private function createWedge(color:uint, arc:Number, startAngle:int):DisplayObject {
			var shape:Shape = new Shape();
			shape.graphics.beginFill(color);
			Wedge.draw(shape.graphics, 0,0, radius, arc, startAngle);
			
			var bitmapData:BitmapData = new BitmapData( radius * 2, radius * 2, true, 0 );
			var transform:Matrix = new Matrix();
			transform.tx = transform.ty = radius;
			bitmapData.draw( shape, transform, null, null, null, true );
			var hit:PixelHitArea = new PixelHitArea ( new Bitmap(bitmapData), .2);
			var texture:Texture = Texture.fromBitmapData( bitmapData, false, false, 1 );
			var display:PixelImageTouch = new PixelImageTouch(texture, hit,0xFF, color);
			display.addEventListener(TouchEvent.TOUCH, onWedgeClicked);
			
			return display;
		}
		
		private function onWedgeClicked(event:TouchEvent):void {
			var pit:PixelImageTouch = event.target as PixelImageTouch;
			if (event.getTouch(pit, TouchPhase.ENDED) != null) {
				var newColor:uint = pit.data as uint;
				if (newColor!= selectedColor) {
					selectColor(newColor);
				}
			}
		}
		
		private function selectColor(color:uint):void {
			var newRotation:Number = calcRotation(findColorIndex(color));
			TweenLite.to(wedges, 1, {rotation:newRotation});
			selectedColor = color;
		}
		
		private function calcRotation(index:int):Number {
			return ((arc*index) * Math.PI / 180);
		}
		
		private function findColorIndex(color:uint):int {
			var index:int = -1;
			for (var i:int = 0; i < colors.length && index == -1; i++) {
				if (color == colors[i]) {
					index = i;
				}
			}
			return index;
		}
		
		public function get selectedColor():uint { return _selectedColor; }
		public function set selectedColor(value:uint):void { 
			_selectedColor = value;
			dispatchEventWith(COLOR_CHANGED);
		}

	}
}